//
//  AddFriendView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/15.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "AddFriendView.h"

@interface AddFriendView()

@property (nonatomic, strong) UIButton *bgView;
@property (nonatomic, strong) UIImageView *user_bg_image;
@property (nonatomic, strong) UIImageView *user_avatar_image;
@property (nonatomic, strong) UILabel *user_name_label;

@property (nonatomic, strong) UIButton *add_friend_button;
@property (nonatomic, weak)   id<AddFriendViewDelegate>delegate;
@property (nonatomic, strong) UserModel *friendModel;

@end

@implementation AddFriendView

+ (AddFriendView *)defaultAddView
{
    static AddFriendView *addFriendView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        addFriendView = [[self alloc] init];
    });
    return addFriendView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = RGBA(0, 0, 0, 0.1);
        
        [self makeAddFriendView];
        
    }
    return self;
}

- (void)makeAddFriendView
{
    _bgView = [UIButton new];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.bottom.mas_equalTo(-40);
        make.width.mas_equalTo(SCREEN_WIDTH / 3);
        make.centerX.mas_equalTo(self);
    }];
    
    _user_bg_image = [UIImageView new];
    _user_bg_image.backgroundColor = [UIColor yellowColor];
    _user_bg_image.contentMode = UIViewContentModeScaleAspectFill;
    _user_bg_image.layer.masksToBounds = YES;
    _user_bg_image.layer.borderWidth = 0.0;
    [_bgView addSubview:_user_bg_image];
    [_user_bg_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(SCREEN_WIDTH / 3);
    }];
    
    _user_avatar_image = [UIImageView new];
    _user_avatar_image.layer.masksToBounds = YES;
    _user_avatar_image.layer.cornerRadius = SCREEN_WIDTH / 12;
    [_user_bg_image addSubview:_user_avatar_image];
    [_user_avatar_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_bgView);
        make.centerY.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 6, SCREEN_WIDTH / 6));
    }];
    
    _user_name_label = [UILabel new];
    _user_name_label.textColor = [UIColor whiteColor];
    _user_name_label.font = [UIFont systemFontOfSize:16.0f];
    _user_name_label.textAlignment = NSTextAlignmentCenter;
    [_user_bg_image addSubview:_user_name_label];
    [_user_name_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_user_avatar_image.mas_bottom).mas_offset(20);
        make.left.right.mas_offset(0);
        make.height.mas_equalTo(15);
    }];
    
    
    UIView *addView = [UIView new];
    [_bgView addSubview:addView];
    [addView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_user_bg_image.mas_bottom).mas_offset(0);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    _add_friend_button = [UIButton new];
    _add_friend_button.layer.masksToBounds = YES;
    _add_friend_button.layer.borderWidth = 1.0;
    _add_friend_button.layer.borderColor = [RGB(220, 220, 220) CGColor];
    _add_friend_button.layer.cornerRadius = 5.0;
    _add_friend_button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [_add_friend_button setTitle:@"加为好友" forState:UIControlStateNormal];
    [_add_friend_button addTarget:self action:@selector(clickAddFriendButton:) forControlEvents:UIControlEventTouchUpInside];
    [_add_friend_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addView addSubview:_add_friend_button];
    [_add_friend_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(addView);
        make.size.mas_equalTo(CGSizeMake(70, 30));
    }];
}

- (void)showAddViewWithFriend:(UserModel *)user delegate:(id<AddFriendViewDelegate>)delegate
{
    self.delegate = delegate;
    self.friendModel = user;
    [_user_bg_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseIp,self.friendModel.background]]];
    [_user_avatar_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseIp,self.friendModel.avatar]]];
    _user_name_label.text = self.friendModel.username;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)hideAddView
{
    [self removeFromSuperview];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hideAddView];
}

- (void)clickAddFriendButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(clickAddFriend:)]) {
        [self.delegate clickAddFriend:self.friendModel];
    }
}

@end
